package com.f1ashback.bus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.f1ashback.bus.Qo.StationQo;
import com.f1ashback.bus.Vo.BusShowInfo;
import com.f1ashback.bus.Vo.StationGet;
import com.f1ashback.bus.Vo.StationVo;
import com.f1ashback.bus.common.AjaxResult;
import com.f1ashback.bus.entity.StaLevel;
import com.f1ashback.bus.entity.Station;
import com.f1ashback.bus.mapper.BusMapper;
import com.f1ashback.bus.mapper.StaLevelMapper;
import com.f1ashback.bus.mapper.StationMapper;
import com.f1ashback.bus.service.StationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private BusMapper busMapper;

    @Autowired
    private StaLevelMapper staLevelMapper;

    @Override
    public List<StationVo> appListSta() {
        log.info("获取Station数据");
        List<Station> stations = stationMapper.selectList(null);
        return resVoList(stations);
    }

    @Override
    public List<StationVo> getStaInfoByStaName(String staName) {
        log.info("获取Station数据");
        List<Station> stations = stationMapper.getStaInfoByStaName(staName);
        return resVoList(stations);
    }

    @Override
    public List<StationVo> listBus() {
        List<StationVo> voList = resVoList(stationMapper.selectList(null));
        return voList;
    }

    @Override
    public AjaxResult getStaName() {
        Map<String, List<BusShowInfo>> res = new HashMap<>();
        List<BusShowInfo> list = stationMapper.getStaName();
        res.put("res", list);
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult deleteSta(Long staId) {
        try {
            stationMapper.deleteById(staId);
        } catch (Exception e) {
            return AjaxResult.error("删除失败！");
        }
        return AjaxResult.success("删除成功！");
    }

    @Override
    public AjaxResult add(StationGet station) {
        //设置车站ID
        try {
            Station stationins = new Station();
            BeanUtils.copyProperties(station, stationins);
            int a = 0;
            for (byte aByte : (station.getStaName() + "f1ash6ack").getBytes()) {
                a += aByte;
            }
            stationins.setStaId(Long.valueOf(a));

            //设置停靠线路ID
            if (station.getBusStop().size() > 0) {
                String busS = "";
                for (String s : station.getBusStop()) {
                    busS += s + ",";
                }
                busS = busS.substring(0, busS.length() - 1);
                stationins.setBusStop(busS);
            } else stationins.setBusStop("暂未设置");

            //设置站点规模和状态
            stationins.setLevel(Integer.valueOf(station.getLevel()));
            stationins.setStatus(Integer.valueOf(station.getStatus()));

            //设置创建时间
            stationins.setCreateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            stationMapper.insert(stationins);
        } catch (BeansException e) {
            return AjaxResult.error("添加失败！");
        }
        return AjaxResult.success("添加成功！");
    }

    @Override
    public AjaxResult updateStation(StationGet stationGet) {
        Station stationupd = new Station();
        BeanUtils.copyProperties(stationGet, stationupd);
        if (stationGet.getBusStop().size() > 0) {
            String busS = "";
            for (String s : stationGet.getBusStop()) {
                busS += s + ",";
            }
            busS = busS.substring(0, busS.length() - 1);
            stationupd.setBusStop(busS);
        }
        if (stationGet.getStatus() != null && stationGet.getStatus().equals("正常")) {
            stationupd.setStatus(1);
        } else {
            stationupd.setStatus(0);
        }
        if (stationGet.getLevel() != null) {
            stationupd.setLevel(staLevelMapper.selectLevelIfByName(stationGet.getLevel()));
        }
        int i = stationMapper.updateById(stationupd);
        if (i <= 0) {
            return AjaxResult.error("修改失败！");
        }
        return AjaxResult.success("修改成功！");
    }

    @Override
    public AjaxResult querySta(StationQo stationQo) {
        Map<String, List<StationVo>> res = new HashMap<>();
        Station queryStaI = new Station();
        BeanUtils.copyProperties(stationQo, queryStaI);
        if (stationQo.getStatus() != null && stationQo.getStatus() != "" && stationQo.getStatus().equals("正常")){
            queryStaI.setStatus(1);
        }else if (stationQo.getStatus() != null && stationQo.getStatus() != "" && stationQo.getStatus().equals("未启用")){
            queryStaI.setStatus(0);
        }else {
            queryStaI.setStatus(null);
        }
        if (stationQo.getLevel() != null && stationQo.getLevel() != ""){
            queryStaI.setLevel(staLevelMapper.selectLevelIfByName(stationQo.getLevel()));
        }
        List<Station> list = stationMapper.querySta(queryStaI);

        res.put("res", resVoList(list));
        return AjaxResult.success(res);
    }

    @Override
    public AjaxResult getStaLevel() {
        Map<String, List<StaLevel>> res = new HashMap<>();
        List<StaLevel> staLevels = staLevelMapper.selectList(null);
        res.put("res", staLevels);
        return AjaxResult.success(res);
    }

    private List<StationVo> resVoList(List<Station> stations) {
        List<StationVo> voList = new ArrayList<>();
        if (stations.isEmpty()) {
            return null;
        }
        for (Station station : stations) {
            if (station != null) {
                StationVo stationVo = new StationVo();
                BeanUtils.copyProperties(station, stationVo);
                if (station.getStatus() != null && station.getStatus() == 1) {
                    stationVo.setStatus("正常");
                } else {
                    stationVo.setStatus("未启用");
                }

                if (!(station.getBusStop().equals("暂未设置"))) {
                    String busStop = station.getBusStop();
                    String[] split = busStop.split(",");
                    String listBusNo = "";
                    for (String s : split) {
                        String tem = busMapper.selectBusNoById(Long.valueOf(s));
                        listBusNo += tem + ",";
                    }
                    listBusNo = listBusNo.substring(0, listBusNo.length() - 1);
                    stationVo.setBusStop(listBusNo);
                }
                String level = staLevelMapper.selectLevelNameById(station.getLevel());
                stationVo.setLevel(level);
                voList.add(stationVo);
            }
        }
        return voList;
    }
}
